package com.ruoyi.ww.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import com.ruoyi.base.mapper.SyTypeMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.ycl.domain.BsJctz;
import com.ruoyi.ycl.domain.vo.BsJctzVo;
import com.ruoyi.ycl.mapper.BsJctzMapper;
import com.ruoyi.ycl.service.IBsJctzService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ww.domain.bo.BsWwsyBo;
import com.ruoyi.ww.domain.vo.BsWwsyVo;
import com.ruoyi.ww.domain.BsWwsy;
import com.ruoyi.ww.mapper.BsWwsyMapper;
import com.ruoyi.ww.service.IBsWwsyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 外委试验信息主Service业务层处理
 *
 * @author zyb
 * @date 2024-08-24
 */
@RequiredArgsConstructor
@Service
public class BsWwsyServiceImpl implements IBsWwsyService {

    private final BsWwsyMapper baseMapper;
    private final BsJctzMapper jctzMapper;
    private final SysDeptMapper deptMapper;
    private final SyTypeMapper syTypeMapper;
    private final IBsJctzService jctzService;

    /**
     * 查询外委试验信息主
     */
    @Override
    public BsWwsyVo queryById(Long wwsyId){
        BsWwsyVo bsWwsyVo = baseMapper.selectVoById(wwsyId);
        fillVo(bsWwsyVo);
        return bsWwsyVo;
    }

    /**
     * 基本填充Vo
     */
    private void fillVo(BsWwsyVo bsWwsyVo) {
        if (ObjUtil.isNotNull(bsWwsyVo.getTzId())) {
            bsWwsyVo.setJctzVo(jctzService.queryById(bsWwsyVo.getTzId()));
        }
        bsWwsyVo.setDeptName(deptMapper.selectVoById(bsWwsyVo.getDeptId()).getDeptName());
        bsWwsyVo.setSyTypeLabel(syTypeMapper.selectVoById(bsWwsyVo.getSyTypeId()).getSyTypeName());

    }

    /**
     * 查询外委试验信息主列表
     */
    @Override
    public TableDataInfo<BsWwsyVo> queryPageList(BsWwsyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsWwsy> lqw = buildQueryWrapper(bo);
        Page<BsWwsyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(this::fillVo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询外委试验信息主列表
     */
    @Override
    public List<BsWwsyVo> queryList(BsWwsyBo bo) {
        LambdaQueryWrapper<BsWwsy> lqw = buildQueryWrapper(bo);
        List<BsWwsyVo> bsWwsyVos = baseMapper.selectVoList(lqw);
        bsWwsyVos.forEach(this::fillVo);
        return bsWwsyVos;
    }

    private LambdaQueryWrapper<BsWwsy> buildQueryWrapper(BsWwsyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsWwsy> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), BsWwsy::getBaogbh, bo.getBaogbh());
        lqw.eq(bo.getSyTypeId() != null, BsWwsy::getSyTypeId, bo.getSyTypeId());
        lqw.eq(bo.getDeptId() != null, BsWwsy::getDeptId, bo.getDeptId());
        lqw.eq(bo.getTzId() != null, BsWwsy::getTzId, bo.getTzId());
        lqw.eq(bo.getBaogrq() != null, BsWwsy::getBaogrq, bo.getBaogrq());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdw()), BsWwsy::getJiancdw, bo.getJiancdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancnr()), BsWwsy::getJiancnr, bo.getJiancnr());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), BsWwsy::getYangpmc, bo.getYangpmc());
        lqw.eq(bo.getWeitrq() != null, BsWwsy::getWeitrq, bo.getWeitrq());
        lqw.eq(StringUtils.isNotBlank(bo.getDengjr()), BsWwsy::getDengjr, bo.getDengjr());
        lqw.eq(StringUtils.isNotBlank(bo.getJianzr()), BsWwsy::getJianzr, bo.getJianzr());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), BsWwsy::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getBeiz()), BsWwsy::getBeiz, bo.getBeiz());
        lqw.eq(StringUtils.isNotBlank(bo.getReportResult()), BsWwsy::getReportResult, bo.getReportResult());
        lqw.orderByDesc(BsWwsy::getBaogrq);
        return lqw;
    }

    /**
     * 新增外委试验信息主
     */
    @Override
    public Boolean insertByBo(BsWwsyBo bo) {
        BsWwsy add = BeanUtil.toBean(bo, BsWwsy.class);
        validEntityBeforeSave(add);
        // 1. 新增进场台账
        long tzId = IdUtil.getSnowflakeNextId();
        BsJctzVo targetJctzVo = new BsJctzVo();
        BsJctzVo sourceJctzVo = bo.getJctzVo();
        if (ObjUtil.isNotNull(sourceJctzVo)) {
            BeanUtil.copyProperties(sourceJctzVo, targetJctzVo);
        }
        targetJctzVo.setTzId(tzId);
        targetJctzVo.setDeptId(bo.getDeptId());
        jctzMapper.insert(BeanUtil.toBean(targetJctzVo, BsJctz.class));
        // 2. 新增外委试验
        add.setTzId(tzId);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改外委试验信息主
     */
    @Override
    public Boolean updateByBo(BsWwsyBo bo) {
        BsWwsy update = BeanUtil.toBean(bo, BsWwsy.class);
        validEntityBeforeSave(update);
        // 1. 更新进场台账
        BsJctzVo jctzVo = bo.getJctzVo();
        jctzVo.setTzId(bo.getTzId());
        jctzVo.setDeptId(bo.getDeptId());
        jctzMapper.updateById(BeanUtil.toBean(jctzVo, BsJctz.class));
        // 2. 更新外委试验
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsWwsy entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除外委试验信息主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        List<BsWwsyVo> bsWwsyVos = baseMapper.selectVoBatchIds(ids);
        List<Long> tzIds = bsWwsyVos.stream().map(BsWwsyVo::getTzId)
            .filter(ObjUtil::isNotNull).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(tzIds)) {
            jctzMapper.deleteBatchIds(tzIds);
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
